import Vue from 'vue'
import Element from 'element-ui'
import axios from 'axios'
import 'element-ui/lib/theme-default/index.css'
import '../public/css/style-output.css'


import Router from 'vue-router';
import tools from './components/mixins/tools'

Vue.mixin(tools);
Vue.use(Element);
Vue.use(Router);
window.axios = axios;
window.host  = 'http://api.chebutou.com.cn';
window.cdn   = 'http://oka19npup.bkt.clouddn.com/';

const router = new Router({
	routes: [
		{
			path: '/login',
			name: 'login',
			component: (resolve) => require(['./components/user/login.vue'], resolve),
			beforeEnter: (to, from, next) => {
                axios.get(host + '/operator/heartbeat', {
                    withCredentials: true,
                    headers: {
                        'Content-Type': 'application/json',
                        'Cache-control': 'no-cache'
                    },
				}).then(res => {
					if (res.data.success) {
						next('/home/tasks')
					} else {
						next()
					}
				})
			},
		},
		{
			path: '/home',
			name: 'home',
			component: (resolve) => require(['./components/home/frame.vue'], resolve),
			beforeEnter: (to, from, next) => {
                axios.get(host + '/operator/heartbeat', {
                    withCredentials: true,
                    headers: {
                        'Content-Type': 'application/json',
                        'Cache-control': 'no-cache'
                    },
				}).then(res =>{
					if (res.data.success) {
						next()
					} else {
						next('/login')
					}
				})
			},
			children: [
				{
					path: 'tasks',
					name: 'tasks',
					component: (resolve) => require(['./components/home/tasks.vue'], resolve),
				},
				{
					path: 'comments',
					name: 'comments',
					component: (resolve) => require(['./components/home/comments.vue'], resolve),
				},
				{
					path: 'creditor',
					name: 'creditor',
					component: (resolve) => require(['./components/home/creditor.vue'], resolve),
				},
				{
					path: 'super-register',
					name: 'super-register',
					component: (resolve) => require(['./components/home/superRegister.vue'], resolve),
				},
				{
					path: 'activity',
					name: 'activity',
					component: (resolve) => require(['./components/home/activity.vue'], resolve),
				},
				{
                    path: 'financial',
                    name: 'financial',
                    component: (resolve) => require(['./components/home/financial.vue'], resolve),
                },
                {
                    path: 'informant',
                    name: 'informant',
                    component: (resolve) => require(['./components/home/informant.vue'], resolve),
                }
			]
		},
		{
			path: '*',
			redirect: '/home/tasks'
		}
	],
	linkActiveClass: 'active'
});

const app = new Vue({
	router
}).$mount('#app');

if (environment === 'dev') {
	Vue.config.devtools = true;
}

// 热重载配置
if (module.hot) {
	module.hot.accept();
}
